package com.zhaolulu.web;
import com.zhaolulu.oss.pojo.Result;
import com.zhaolulu.util.AliyunOSSOperator;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


@RestController
@Slf4j
//@RequestMapping("/file")
@Api(tags = "文件上传管理")
public class OSSController {

    @Resource
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
//    localhost:2025/file/upload
    public Result upload(MultipartFile file) throws Exception {
        log.info("文件上传: {}", file.getOriginalFilename());
        //将文件交给OSS存储管理
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传OSS, url: {}", url);

        //将文件交给MinIO存储管理
//        String url= minIOUtils.uploadFile(file);
//        log.info("文件上传MinIO, url: {}", url);
        return Result.success(url);
    }
//
//    //阿里云存储
//    @PostMapping("/fileUpload")
//    @ResponseBody
//    @ApiOperation(value = "文件上传", notes = "上传文件到minio存储")
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "上传成功"),
//            @ApiResponse(code = 500, message = "上传失败")
//    })
//    public String fileUpload(
//            @ApiParam(name = "fileName", value = "上传的文件", required = true)
//            @RequestParam("fileName") MultipartFile image) {
//        // 在上传前检查存储桶是否存在
//        try {
//            boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder()
//                    .bucket(minIOConfig.getBucket())
//                    .build());
//
//            if (!bucketExists) {
//                // 创建存储桶
//                minioClient.makeBucket(MakeBucketArgs.builder()
//                        .bucket(minIOConfig.getBucket())
//                        .build());
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return minIOUtils.uploadFile(image);
//    }

}
